require "./service.cr"

begin
	simulation, no_configuration, configuration_file = Baguette::Configuration.option_parser

	configuration = if no_configuration
		Baguette::Log.info "do not load a configuration file."
		Baguette::Configuration::Auth.new
	else
		Baguette::Configuration::Auth.get(configuration_file) ||
			Baguette::Configuration::Auth.new
	end

	Baguette::Context.verbosity = configuration.verbosity

	if key_file = configuration.secret_key_file
		configuration.secret_key = File.read(key_file).chomp
	end

	OptionParser.parse do |parser|
		parser.banner = "usage: authd [options]"

		parser.on "--storage-directory directory", "Directory in which to store users." do |directory|
			Baguette::Log.info "Storage directory: #{directory}."
			configuration.storage_directory = directory
		end

		parser.on "--service-name service_name", "Service name (IPC)." do |name|
			Baguette::Log.info "Service name: #{name}."
			configuration.service_name = name
		end

		parser.on "--recreate-indexes", "Recreate database indexes (symbolic links)." do
			Baguette::Log.info "Recreate indexes."
			configuration.recreate_indexes = true
		end

		parser.on "-k file", "--key-file file", "JWT key file" do |file_name|
			Baguette::Log.info "Secret key: #{file_name}."
			configuration.secret_key_file = file_name
			configuration.secret_key = File.read(file_name).chomp
		end

		parser.on "-R", "--allow-registrations", "Allow user registration." do
			Baguette::Log.info "Allow registrations."
			configuration.registrations = true
		end

		parser.on "-E", "--require-email", "Require an email." do
			Baguette::Log.info "Require an email address for registration."
			configuration.require_email = true
		end

		parser.on "-t activation-template-name", "--activation-template name", "Email activation template." do |opt|
			Baguette::Log.info "Activation template name: #{opt}."
			configuration.activation_template = opt
		end

		parser.on "-r recovery-template-name", "--recovery-template name", "Email recovery template." do |opt|
			Baguette::Log.info "Recovery template name: #{opt}."
			configuration.recovery_template = opt
		end

		parser.on "-m mailer-exe", "--mailer mailer-exe", "Application to send registration emails." do |opt|
			Baguette::Log.info "Mailer: #{opt}."
			configuration.mailer_exe = opt
		end

		parser.on "-x key", "--read-only-profile-key key", "Marks a user profile key as being read-only." do |key|
			Baguette::Log.info "Read-only key in user profiles: #{key}."
			configuration.read_only_profile_keys.push key
		end

		parser.on "-h", "--help", "Show this help" do
			puts parser
			exit 0
		end
	end

	if simulation
		pp! configuration
		exit 0
	end

	if path = configuration.log_file
		Baguette::Log.log_path = path
	end

	AuthD::Service.new(configuration).run

rescue e : OptionParser::Exception
	Baguette::Log.error e.message
rescue e
	Baguette::Log.error "exception raised: #{e.message}"
	e.backtrace.try &.each do |line|
		STDERR << "  - " << line << '\n'
	end
end
